package com.spbdx.studyspringboot.duty_chain.chain;

import com.spbdx.studyspringboot.duty_chain.annotation.Duty;
import com.spbdx.studyspringboot.duty_chain.entity.Product;
import org.springframework.core.annotation.AnnotationUtils;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @Author: fan
 * @CreateTime 2025/1/1 22:34
 * @Description
 * @Version 1.0
 */
/**
 * @Author: fan
 * @CreateTime 2025/1/1 22:34
 * @Description
 * @Version 1.0
 */
public class ProductChainFactory {

    private Map<String, List<IHandler<Product, Boolean>>> handlerMap = new HashMap<>();


    // 这里是把所有的实现类注入进来了
    public void ProductChainFactory(List<IHandler<Product,Boolean>> handlers){


        Map<String, List<IHandler<Product, Boolean>>> collect = handlers.stream().sorted(Comparator.comparingInt(h -> AnnotationUtils.findAnnotation(h.getClass(), Duty.class).order()))
                .flatMap(handler -> {
                    Duty duty = AnnotationUtils.findAnnotation(handler.getClass(), Duty.class);
                    if (duty != null && duty.type() != null) {
                        return Arrays.stream(duty.type()).map(type -> Map.entry(type, handler));
                    } else {
                        return Stream.empty();
                    }
                })
                // 分组成map
                .collect(Collectors.groupingBy(
                        entry -> entry.getKey(),
                        Collectors.mapping(entry -> entry.getValue(), Collectors.toList())
                ));


    }

//    public ProductChainFactory(List<IHandler<Product, Boolean>> handlers) {
//        // Sort handlers by the order specified in the Duty annotation
//        handlers.sort(Comparator.comparingInt(handler ->
//                AnnotationUtils.findAnnotation(handler.getClass(), Duty.class)
//                        .order()
//
//        // Create a mapping from product type to handlers
//        for (IHandler<Product, Boolean> handler : handlers) {
//            Duty duty = AnnotationUtils.findAnnotation(handler.getClass(), Duty.class);
//            if (duty != null && duty.type() != null) {
//                for (String type : duty.type()) {
//                    handlerMap.computeIfAbsent(type, k -> new ArrayList<>()).add(handler);
//                }
//            }
//        }
//    }

//    public IHandler<Product, Boolean> getHandlerChain(String productType) {
//        List<IHandler<Product, Boolean>> handlers = handlerMap.getOrDefault(productType, Collections.emptyList());
//        return createChain(handlers);
//    }
//
//    private IHandler<Product, Boolean> createChain(List<IHandler<Product, Boolean>> handlers) {
//        if (handlers.isEmpty()) {
//            return null;
//        }
//
//        IHandler<Product, Boolean> head = handlers.get(0);
//        IHandler<Product, Boolean> current = head;
//
//        for (int i = 1; i < handlers.size(); i++) {
//            current.setNext(handlers.get(i));
//            current = current.getNext();
//        }
//
//        return head;
//    }
}
